package pkg.chemdrm;

import java.util.ConcurrentModificationException;
import java.util.LinkedList;

import org.anddev.andengine.engine.camera.BoundCamera;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.camera.hud.HUD;
import org.anddev.andengine.engine.handler.timer.ITimerCallback;
import org.anddev.andengine.engine.handler.timer.TimerHandler;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXObject;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.Scene.IOnSceneTouchListener;
import org.anddev.andengine.entity.shape.IShape;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.entity.sprite.BaseSprite;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.text.Text;
import org.anddev.andengine.entity.text.TickerText;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.input.touch.controller.ITouchController.ITouchEventCallback;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.util.HorizontalAlign;

import android.content.Context;

import android.widget.Toast;

public class GameScene extends ChemScene implements ITimerCallback, IOnSceneTouchListener {
	private final static float FPS = 60; // try to force this FPS; note that it is possible to fall below this frame rate.
	private final static float FRAME_PERIOD = 1f/FPS; // inverted this is in seconds per frame
	 
	// the camera velocities will go away when we have a player, and we bind the camera to it.
	//private final static float CAMERA_X_VEL = 32; // in pixels per second
	//private final static float CAMERA_Y_VEL = 32; // in pixels per second
	
	private final static float HORZ_MOVE_BUTTON_WIDTH = 151; // in pixels
	private final static float VERT_MOVE_BUTTON_HEIGHT = 85; // in pixels
	
	private final static float THROW_DELAY = 0.5f;
	
	private TimerHandler 		mTimerHandler;
	private EntityHandler		mEntityHandler;
	private InventoryHandler	mInventoryHandler;
	
	private Map mMap;
	private Player mPlayer;
	private Inventory mInventory;
	private HUD mHud; 
	private AIHandler mAIHandler;
	
	/* Touch Related */
	
	private float mCameraXVel;
	private float mCameraYVel;
	
	private boolean mTouchFlag = false;
	private boolean mIsDown = false;
	private float mLastTouchX = 0;
	private float mLastTouchY = 0;
	private float mRunTime = 0;
	private float mLastTouchTime = 0;
	private float mHoldTime = 0;
	private float throwDelayTimer = THROW_DELAY;
	private ResourceHandler mResourceHandler;
	private float mAddTimeBegin = 0;
	private Text mTicketText = null;
	
	private boolean mArmed = false;
	
	private LinkedList<EntityFlask> mFlasks = new LinkedList<EntityFlask>();
	
	public GameScene(final Context pContext, final SceneHandler pSceneHandler, final ResourceHandler pResourceHandler, final BoundCamera pCamera)
	{
		super(pContext, pSceneHandler, pResourceHandler, pCamera);

		// this tells the engine to call the method onTimePassed every FRAME_PERIOD seconds (auto repeat) 
		mTimerHandler = new TimerHandler(FRAME_PERIOD, true, this);
		mEntityHandler = new EntityHandler(this);
		mHud = new HUD();
		mInventoryHandler = new InventoryHandler(mHud, pResourceHandler);
		mAIHandler = new AIHandler(this);
		
		mResourceHandler = pResourceHandler;
		
		//pCamera.setHUD(mHud);
		
		mInventory = new  Inventory(pResourceHandler, mInventoryHandler, 50, 25, 380, 270, pResourceHandler.getRegion("NOTHING")); 
		mInventory.setAlpha(128);
		mHud.attachChild(mInventory);
		mInventory.hideInventory();
		
		
		registerUpdateHandler(mTimerHandler);
		setOnSceneTouchListener(this);

		buildScene();
	}
	
	/* 
	 * (non-Javadoc)
	 * @see org.anddev.andengine.engine.handler.timer.ITimerCallback#onTimePassed(org.anddev.andengine.engine.handler.timer.TimerHandler)
	 * All of the game code goes in this method. We will update AI, and carry out the trigger stuff, all leads back to this point.
	 */
	public void onTimePassed(final TimerHandler pTimerHandler) 
	{
		//float dt = pTimerHandler.getTimerSecondsElapsed();
		float dt = 1f/60f;
		
		mRunTime += dt;
		throwDelayTimer += dt;
		
		mPlayer.setCaffeinated(false);
		
		if(mRunTime - mAddTimeBegin >= 30)
		{
			if(mTicketText != null)
				mHud.detachChild(mTicketText);
		}
		
		if(mInventoryHandler.getActiveSubstance() != null)
		{
			String s = mInventoryHandler.getActiveSubstance().mName;
			
			if(s.equals("Caffeine"))
			{
				
				mPlayer.setCaffeinated(true);
			}
	
		}
		
		if(mIsDown)
		{
			//System.out.println("mHoldTime: " + mHoldTime);
			mHoldTime += dt;

			if(mHoldTime >= 1 && mTouchFlag) // open the inventory
			{
				// then we're either trying to access the menu or throw a flask.
				mInventory.showInventory();
				mHud.registerTouchArea(mInventory);
				
			}
		}
		// this gets called every frame (when this scene is active)
		
		mCamera.setCenter(mPlayer.getX() + 48, mPlayer.getY() + 48);
		mEntityHandler.updateEntities(dt); // updates player and other things.
		mAIHandler.update(dt);
		
		checkZLevel(mPlayer);
		checkPitCollision(mPlayer);
		checkScript(mPlayer);
		checkDarkess(mPlayer);
		checkFlaskDoorCollision();
		
	}
	
	public void checkFlaskDoorCollision()
	{
	
		try
		{
			for(EntityFlask f : this.mFlasks)
			{
				if(!f.getExists())
					continue;
				
				float Ax1 = f.getX();
				float Ay1 = f.getY();
				float Ax2 = f.getX() + f.getWidth();
				float Ay2 = f.getY() + f.getHeight();
				
				if(mInventoryHandler.getActiveSubstance().mName.equals("Oxygen"))
				{
					for(DoorSprite z : mMap.mDoorList)
					{
						float Bx1 = z.getX();
						float By1 = z.getY();
						
						float Bx2 = z.getX() + z.getWidth();
						float By2 = z.getY() + z.getHeight();
						
						
						if (Ax1 < Bx2 && Ax2 > Bx1 && Ay1 < By2 && Ay2 > By1)
						{
							f.destroySelf();
							z.destroySelf();
						}
					}
				}
				
				for(AIPlayer z : mAIHandler.mAIPlayers.values())
				{
					float Bx1 = z.getX();
					float By1 = z.getY();
					
					float Bx2 = z.getX() + z.getWidth();
					float By2 = z.getY() + z.getHeight();
					
					
					if (Ax1 < Bx2 && Ax2 > Bx1 && Ay1 < By2 && Ay2 > By1)
					{
						boolean flag = (z.mType.equals("Slug") && mInventoryHandler.getActiveSubstance().mName.equals("Salt"))
								||(z.mType.equals("Fireball") && mInventoryHandler.getActiveSubstance().mName.equals("Water"))
								||(z.mType.equals("Robot") && mInventoryHandler.getActiveSubstance().mName.equals("Oxygen"))
								||(z.mType.equals("Slime") && mInventoryHandler.getActiveSubstance().mName.equals("Salt"));
										
						
						if(flag)
						{
							f.destroySelf();
							z.destroySelf();
						}
					}
				}
				
				
				
				float Bx1 = mMap.mBoss.getX();
				float By1 = mMap.mBoss.getY();
				float Bx2 = mMap.mBoss.getX() + mMap.mBoss.getWidth();
				float By2 = mMap.mBoss.getY() + mMap.mBoss.getHeight();
				
				if (Ax1 < Bx2 && Ax2 > Bx1 && Ay1 < By2 && Ay2 > By1)
				{
					mMap.mBoss.detachSelf();
					f.destroySelf();
				}
				
				
				
				
			}
		}
		catch(ConcurrentModificationException e)
		{
			// do nothing.
		}
					
	}
	
	// this function is necessary for awhile, since I guess AndEngine doesn't support a "setCamera"
	public void restoreCamera()
	{
		mCamera.setCenter(mPlayer.getX() + 48, mPlayer.getY() + 48);
		mCamera.setHUD(mHud);
		
	}
	

	public void checkDarkess(IShape pShape)
	{
		float Ax1 = pShape.getX();
		float Ay1 = pShape.getY();
		float Ax2 = pShape.getX() + pShape.getWidth();
		float Ay2 = pShape.getY() + pShape.getHeight();
				
		for(AIRegion z : mMap.mDarknessList)
		{
			float Bx1 = z.getX();
			float By1 = z.getY();
			
			float Bx2 = z.getX() + z.getWidth();
			float By2 = z.getY() + z.getHeight();
			
			if (Ax1 < Bx2 && Ax2 > Bx1 && Ay1 < By2 && Ay2 > By1)
			{
				mMap.setColor(128f/255f, 64f/255f, 128f/255f);
				mPlayer.setTorch(true);
				break;
			}
			else
			{
				mMap.setColor(202f/255f, 171f/255f, 204f/255f);
				mPlayer.setTorch(false);
			}
			
		}
		
		float Bx1 = mMap.mEnd.getX();
		float By1 = mMap.mEnd.getY();
		
		float Bx2 = mMap.mEnd.getX() + mMap.mEnd.getWidth();
		float By2 = mMap.mEnd.getY() + mMap.mEnd.getHeight();
	
		if (Ax1 < Bx2 && Ax2 > Bx1 && Ay1 < By2 && Ay2 > By1)
		{
			mSceneHandler.setScene("MainMenu");
		}
	
		
				
	}
	
	

	
	public void checkPitCollision(IShape pShape)
	{
		float Ax1 = pShape.getX();
		float Ay1 = pShape.getY();
		float Ax2 = pShape.getX() + pShape.getWidth();
		float Ay2 = pShape.getY() + pShape.getHeight();
		
		
		for(ZRegion z : mMap.mPitsList)
		{
			float Bx1 = z.mRegion.getX();
			float By1 = z.mRegion.getY();
			float Bx2 = z.mRegion.getX() + z.mRegion.getWidth();
			float By2 = z.mRegion.getY() + z.mRegion.getHeight();
			
			if (Ax1 < Bx2 && Ax2 > Bx1 && Ay1 < By2 && Ay2 > By1)
			{
				//System.err.println("ZCollision!");
				if(mPlayer.getScaleX() == 1 && !mPlayer.mIsCaffeinated)
				{
					// player dies
					mPlayer.setPosition(mMap.getStartX(), mMap.getStartY());

				}
			}
		}
				
	}
	
	
	
	public void checkZLevel(IShape pShape)
	{
		float Ax1 = pShape.getX();
		float Ay1 = pShape.getY();
		float Ax2 = pShape.getX() + pShape.getWidth();
		float Ay2 = pShape.getY() + pShape.getHeight();
		
		
		for(ZRegion z : mMap.mZRegionList)
		{
			float Bx1 = z.mRegion.getX();
			float By1 = z.mRegion.getY();
			float Bx2 = z.mRegion.getX() + z.mRegion.getWidth();
			float By2 = z.mRegion.getY() + z.mRegion.getHeight();
			
			if (Ax1 < Bx2 && Ax2 > Bx1 && Ay1 < By2 && Ay2 > By1)
			{
				//System.err.println("ZCollision!");
				if(mPlayer.mIsCaffeinated)
				{
					mPlayer.setScale(1+(z.mZ/10f));
					if(z.mZ == 1)
					{
						mPlayer.setScale(1);
					}
				}
			}
		}
				
	}
	
	public void checkScript(IShape pShape)
	{
		float Ax1 = pShape.getX();
		float Ay1 = pShape.getY();
		float Ax2 = pShape.getX() + pShape.getWidth();
		float Ay2 = pShape.getY() + pShape.getHeight();
		
		LinkedList<ScriptEntity> trash = new LinkedList<ScriptEntity>();
		
		
		for(ScriptEntity z : mMap.mScriptList)
		{
			float Bx1 = z.mRegion.getX();
			float By1 = z.mRegion.getY();
			float Bx2 = z.mRegion.getX() + z.mRegion.getWidth();
			float By2 = z.mRegion.getY() + z.mRegion.getHeight();
			
			if (Ax1 < Bx2 && Ax2 > Bx1 && Ay1 < By2 && Ay2 > By1)
			{
				//System.err.println("SCRIPT COLLISION!");
				
				if(mTicketText != null)
				{
					mHud.detachChild(mTicketText);
				}
				
				if(z.scriptNum == 1) //Spawn Intro
				{
					//Toast.makeText(this.mContext, "Touch & Drag the face!", Toast.LENGTH_LONG).show();
					System.err.println("Z is " + z.scriptNum);
					mTicketText = new TickerText(0, 0, mResourceHandler.getFont("Inventory"), "That was quite the blast you were in. \nYou're currently in a dream state and need to reach the end of the level\n in order to awaken.\n\n In this world you have access to your knowledge of chemicals (long press on \nthe player). \nYou will need to use that knowledge to help you survive.", HorizontalAlign.LEFT, 10);
					trash.add(z);
					mHud.attachChild(mTicketText);
					this.mAddTimeBegin = mRunTime;
				}
				else if(z.scriptNum == 2) //Robot Intro
				{
					System.err.println("Z is " + z.scriptNum);
					mTicketText = new TickerText(0, 0, mResourceHandler.getFont("Inventory"), "You have encountered a robot with a corrupted OS. \nLooks like you'll need to figure out a way to get past him. \n\n HINT: You could use a flask of oxygen on him and cause him to rust away.", HorizontalAlign.LEFT, 10);
					trash.add(z);
					mHud.attachChild(mTicketText);
					this.mAddTimeBegin = mRunTime;
				}
				else if(z.scriptNum == 3) //Iron door
				{
					System.err.println("Z is " + z.scriptNum);
					mTicketText = new TickerText(0, 0, mResourceHandler.getFont("Inventory"), "Well that looks like quite the door. \n\nThankfully we are in a dream and it can be rusted instantly. \nThrow some oxygen at the door in order to rust it.", HorizontalAlign.LEFT, 10);
					trash.add(z);
					mHud.attachChild(mTicketText);
					this.mAddTimeBegin = mRunTime;
				}
				else if(z.scriptNum == 4) //slime Intro
				{
					System.err.println("Z is " + z.scriptNum);
					mTicketText = new TickerText(0, 0, mResourceHandler.getFont("Inventory"), "These guys look mean! \nHit them with salt which is made up of sodium and chlorine.", HorizontalAlign.LEFT, 10);
					trash.add(z);
					mHud.attachChild(mTicketText);
					this.mAddTimeBegin = mRunTime;
				}
				else if(z.scriptNum == 5) //Ramp intro
				{
					//Toast.makeText(this.mContext, "RAMP BITCH!", Toast.LENGTH_LONG).show();
					mTicketText = new TickerText(0, 0, mResourceHandler.getFont("Inventory"), "To get over these ramps you'll need caffeine. \n Combine hydrogen, carbon, and nitrogen to get caffeine. \n This increases your movement speed.", HorizontalAlign.LEFT, 10);
					trash.add(z);
					mHud.attachChild(mTicketText);
					this.mAddTimeBegin = mRunTime;
				}
				else if(z.scriptNum == 6) //Darkness intro
				{
					System.err.println("Z is " + z.scriptNum);
					mTicketText = new TickerText(0, 0, mResourceHandler.getFont("Inventory"), "It's pretty dark in here! \n Good thing you have that torch with you!", HorizontalAlign.LEFT, 10);
					trash.add(z);
					mHud.attachChild(mTicketText);
					this.mAddTimeBegin = mRunTime;
				}
				else if(z.scriptNum == 7) //fireball intro
				{
					System.err.println("Z is " + z.scriptNum);
					mTicketText = new TickerText(0, 0, mResourceHandler.getFont("Inventory"), "That thing is on fire!\n Hit it with some water! (Mix hydrogen and oxygen)", HorizontalAlign.LEFT, 10);
					trash.add(z);
					mHud.attachChild(mTicketText);
					this.mAddTimeBegin = mRunTime;
				}
				else if(z.scriptNum == 8) //fog intro
				{
					System.err.println("Z is " + z.scriptNum);
					mTicketText = new TickerText(0, 0, mResourceHandler.getFont("Inventory"), "It's foggy! \n Watch your step for enemies. It may be wiser\n to just bypass them.", HorizontalAlign.LEFT, 10);
					trash.add(z);
					mHud.attachChild(mTicketText);
					this.mAddTimeBegin = mRunTime;
				}
				else if(z.scriptNum == 9) //slug intro
				{
					System.err.println("Z is " + z.scriptNum);
					mTicketText = new TickerText(0, 0, mResourceHandler.getFont("Inventory"), "Look at this menacing slug in the fog! \nKill it with salt! Just mix some sodium and chlorine to get salt.", HorizontalAlign.LEFT, 10);
					trash.add(z);
					mHud.attachChild(mTicketText);
					this.mAddTimeBegin = mRunTime;
				}
				else if(z.scriptNum == 10) //boss intro
				{
					System.err.println("Z is " + z.scriptNum);
					mTicketText = new TickerText(0, 0, mResourceHandler.getFont("Inventory"), "LOOK at that giant Gnome! \nHit him with anything you've got!", HorizontalAlign.LEFT, 10);
					trash.add(z);
					mHud.attachChild(mTicketText);
					this.mAddTimeBegin = mRunTime;
				}
			}
			

		}
		
		for(ScriptEntity s : trash)
		{
			mMap.mScriptList.remove(s);
			
		}
				
	}
	

	private void buildScene()
	{
		// load map?
		
		
		//System.err.println("GameScene before::GameScene has " + this.getChildCount() + " kids.");

		mMap = new Map(mAIHandler, this, mResourceHandler);
				
		//System.err.println("There will be " + mAIHandler.mAIPlayers.size());
		
		//mAIHandler.addAI(new AIPlayer(new AIRegion(1928, 8970, 14.812f*32, 7.562f*32), 1928, 8970, 96, 107, mResourceHandler.getAnimatedRegion("Robot")));

		for(AIPlayer p : mAIHandler.mAIPlayers.values())
		{
			attachChild(p);
		}
	//	System.err.println("GameScene after::GameScene has " + this.getChildCount() + " kids.");

		
		mCamera.setBounds(0, mMap.getMapWidth(), 0, mMap.getMapHeight());
		mCamera.setBoundsEnabled(true);
		
		// set the player up
		this.attachChild(mMap.mBoss);
		
		mPlayer = new Player(mResourceHandler, mMap);
		attachChild(mPlayer);		
		mEntityHandler.spawnEntity(mPlayer);
		
		for(DoorSprite s : mMap.mDoorList)
		{
			attachChild(s);
			mEntityHandler.spawnEntity(s);
		}
		
		
		for(FogEntity s : mMap.mFogList)
		{
			attachChild(s);
			mEntityHandler.spawnEntity(s);
		}
		
		
		
		mPlayer.setPosition(mMap.getStartX(), mMap.getStartY());
		mPlayer.setScaleX(1.0f);

	}
	
	// I wrote this since the one in AndEngine is behaving oddly.
	public boolean checkPointInSprite(BaseSprite pSprite, float x, float y)
	{
		float minX = pSprite.getX();
		float minY = pSprite.getY();
		
		float maxX = pSprite.getX() + pSprite.getWidth();
		float maxY = pSprite.getY() + pSprite.getHeight();

		if(minX <= x && x <= maxX)
		{
			
			if(minY <= y  && y <= maxY)
			{
				return true;
			}

			
		}

		return false;
	}

	public boolean onSceneTouchEvent(Scene mScene, TouchEvent pTouch) 
	{
		//System.out.println("Touch!");
		float x = pTouch.getX();
		float y = pTouch.getY();
		
		float cx = mCamera.getMinX();
		float cy = mCamera.getMinY();
		
		mIsDown = pTouch.isActionMove() || pTouch.isActionDown();
		
		if(mIsDown)
		{
			
			if(!mInventory.isInventoryOpen() && !mArmed)
			{
				// upper left
				if(x >= 0 - cx && x - cx <= HORZ_MOVE_BUTTON_WIDTH && y - cy >= 0 && y - cy <= VERT_MOVE_BUTTON_HEIGHT)
				{
					mPlayer.setMove(-1, -1, Player.PLAYER_DIR_UP_LEFT);
				}
				// top
				else if(x - cx > HORZ_MOVE_BUTTON_WIDTH && x - cx < (mCamera.getWidth() - HORZ_MOVE_BUTTON_WIDTH) && y - cy >= 0 && y - cy <= VERT_MOVE_BUTTON_HEIGHT)
				{
					mPlayer.setMove(0, -1, Player.PLAYER_DIR_UP);
				}
				// upper right
				else if(x - cx >= (mCamera.getWidth() - HORZ_MOVE_BUTTON_WIDTH) && x - cx < mCamera.getWidth() && y - cy >= 0 && y - cy<= VERT_MOVE_BUTTON_HEIGHT)
				{
					mPlayer.setMove(1, -1, Player.PLAYER_DIR_UP_RIGHT);
				}
				// right
				else if(x - cx > (mCamera.getWidth() - HORZ_MOVE_BUTTON_WIDTH) && x - cx < mCamera.getWidth() && y - cy >= VERT_MOVE_BUTTON_HEIGHT && y - cy < (mCamera.getHeight() - VERT_MOVE_BUTTON_HEIGHT))
				{
					mPlayer.setMove(1, 0, Player.PLAYER_DIR_RIGHT);
				}
				// bottom right
				else if(x - cx> (mCamera.getWidth() - HORZ_MOVE_BUTTON_WIDTH) && x - cx < mCamera.getWidth() && y - cy >= (mCamera.getHeight() - VERT_MOVE_BUTTON_HEIGHT) && y - cy < mCamera.getHeight())
				{
					mPlayer.setMove(1, 1, Player.PLAYER_DIR_DOWN_RIGHT);
				}			
				// bottom
				else if(x - cx > HORZ_MOVE_BUTTON_WIDTH && x - cx < (mCamera.getWidth() - HORZ_MOVE_BUTTON_WIDTH) && y - cy >= (mCamera.getHeight() - VERT_MOVE_BUTTON_HEIGHT) && y - cy< mCamera.getHeight())
				{
					mPlayer.setMove(0, 1, Player.PLAYER_DIR_DOWN);
				}
				// bottom left
				else if(x - cx > 0 && x - cx < HORZ_MOVE_BUTTON_WIDTH && y - cy >= (mCamera.getHeight() - VERT_MOVE_BUTTON_HEIGHT) && y - cy < mCamera.getHeight())
				{
					mPlayer.setMove(-1, 1, Player.PLAYER_DIR_DOWN_LEFT);
				}
				// left
				else if(x - cx > 0 && x - cx < HORZ_MOVE_BUTTON_WIDTH && y - cy > VERT_MOVE_BUTTON_HEIGHT && y - cy < (mCamera.getHeight() - VERT_MOVE_BUTTON_HEIGHT))
				{
					mPlayer.setMove(-1, 0, Player.PLAYER_DIR_LEFT);
				}
				else
				{
					// we're not pressing any buttons so just give it up.
					mPlayer.stopMoving();
				}
				
				if(!mInventory.isInventoryOpen())
				{
					if(checkPointInSprite(mPlayer, x, y))
					{
						mTouchFlag = true;

					}
					else
					{
						mTouchFlag = false;
					}
				}
			}
			

			
		}
		else if(pTouch.isActionUp())
		{
			if(!checkPointInSprite(mPlayer, pTouch.getX(), pTouch.getY()))
			{
				if(mArmed)
				{	
					if(throwDelayTimer >= THROW_DELAY)
					{
						
						System.err.println("Throwing flask!!!");
						if(Math.abs(x - mCamera.getCenterX()) != 0)
						{
							EntityFlask newFlask = new EntityFlask((float)Math.atan2(y - mCamera.getCenterY(), x - mCamera.getCenterX()),  mPlayer.getX() + 48, mPlayer.getY() + 48, 32, 48, mResourceHandler.getRegion("Flask"));
							attachChild(newFlask);
							mFlasks.add(newFlask);
							mEntityHandler.spawnEntity(newFlask);
						}
						
						throwDelayTimer = 0;
						mArmed 			= false;
					}
				}

			}
			else
			{
				mArmed = true;
			}
			
			mPlayer.stopMoving();
			mTouchFlag = false;
			mLastTouchTime = 0;
			mHoldTime = 0;
		}

		
			
		if(!checkPointInSprite(mInventory, x - mCamera.getMinX(), y - mCamera.getMinY()))
		{
			mInventory.hideInventory();			
			mHud.unregisterTouchArea(mInventory);
		}
		


			

		mLastTouchX = x;
		mLastTouchY = y;
		mLastTouchTime = mRunTime;
		
		return true; // no clue what this does
	}

	
}
